/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.commands.create;

import com.ewerp.mud.commands.BaseCommand;
import com.ewerp.mud.commands.ICommand;
import com.ewerp.mud.core.persistence.IPersistenceManager;
import com.ewerp.mud.core.persistence.IPersistenceSession;
import com.ewerp.mud.core.session.parsers.TerminalLineParser.ITerminalLinePacketParser;
import com.ewerp.mud.entities.IEntity;
import com.ewerp.mud.messages.BaseMessage;
import com.ewerp.mud.messages.IMessage;

/**
 * This class will create an instance of an object.
 * It should be populated by subsequent calls to:
 * setProperty id name value
 * Where id is the id of the object to be set.
 * Where name is the name of the property to set.
 * Where value is the value of the property to set.
 * 
 * @author cboyden
 *
 */

public class CreateCommand extends BaseCommand {
    public static class CreateTerminalLinePacketParser implements ITerminalLinePacketParser  {
        @Override
        public ICommand parseTerminalLinePacket(IEntity source, String input) {
            
            String[] tokens = input.split(" ");
            
            if(tokens.length > 1) {
                CreateCommand cmd = new CreateCommand();
                cmd.setSource(source);
                cmd.setItemType(tokens[1]);
                return cmd;
            }
            return null;
        }
    }
    
    protected IEntity source;
    protected String itemType;
    
    @Override
    public void execute() throws IllegalStateException {
        try {
            IPersistenceSession persistence = ((IPersistenceManager)getPluginManager().getPlugin(IPersistenceManager.class)).openSession();
            Class<?> clazz = getClass().getClassLoader().loadClass(getItemType());
            if(IEntity.class.isAssignableFrom(clazz)) {
                IEntity obj = (IEntity)clazz.newInstance();
                persistence.store(obj);
                IMessage msg = new BaseMessage();
                msg.setText("ObjectId: " + obj.getObjectId());
                if(source != null) {
                    source.processMessage(msg);
                }
                persistence.commit();
            } else {
                // TODO: Error to caller. Bad object type
                System.err.println("Creation error");
            }
        } catch (ClassNotFoundException e) {
            // TODO: Notify caller that the class cannot be loaded
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getItemType() {
        return itemType;
    }

    public void setItemType(String itemType) {
        this.itemType = itemType;
    }

    public IEntity getSource() {
        return source;
    }

    public void setSource(IEntity source) {
        this.source = source;
    }

}
